package net.tksl.crm.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import net.tksl.crm.dao.PaisDao;
import net.tksl.crm.dao.bd.CiudadBD;
import net.tksl.crm.dao.bd.PaisBD;

@Repository("paisDaoImpl")
public class PaisDaoImpl implements PaisDao {

	@Autowired
	@Qualifier("sessionFactory")
	private SessionFactory sessionFactory;
	
	@SuppressWarnings("unchecked")
	@Override
	public List<PaisBD> listarPaises() {
		String hql = "from PaisBD p order by p.nombrePais asc";
		Session sesion = sessionFactory.getCurrentSession();
		Query query = sesion.createQuery(hql);
		List<PaisBD> lista = (List<PaisBD>)query.list();
		return lista;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CiudadBD> listarCiudadesDePais(String codigoPais) {
		String hql = "from CiudadBD c where c.pais.codigoPais = ? order by c.nombreCiudad asc";
		Session sesion = sessionFactory.getCurrentSession();
		Query query = sesion.createQuery(hql);
		
		int i = 0;
		query.setParameter(i,codigoPais);i++;
		
		List<CiudadBD> lista = (List<CiudadBD>)query.list();
		return lista;
	}

}
